package www.sofn.com.goods;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import www.sofn.com.domain.R;
import www.sofn.com.domain.vo.PageRestVO;
import www.sofn.com.goods.domain.query.GoodsQueryVO;
import www.sofn.com.goods.domain.po.GoodsPO;
import www.sofn.com.goods.domain.vo.GoodsListVO;
import www.sofn.com.goods.domain.vo.GoodsVO;
import www.sofn.com.validate.group.InsertGroup;
import www.sofn.com.validate.group.UpdateGroup;

@RestController
@RequestMapping("goods")
@Tag(name = "商品管理")
public class GoodsController {

    @Resource
    private GoodsService service;

    @Operation(summary = "商品列表")
    @GetMapping
    public R<PageRestVO<GoodsListVO>> listPage(GoodsQueryVO query) {
        return R.ok(service.listPage(query));
    }

    @Operation(summary = "获取商品详情")
    @GetMapping("/{id}")
    public R<GoodsVO> getById(@PathVariable(value = "id", required = true) String id) {
        return R.ok(service.getById(id));

    }

    @Operation(summary = "新增商品")
    @PostMapping
    public R insert(@RequestBody @Validated(value = {InsertGroup.class}) GoodsVO goodsVO) {
        service.insert(BeanUtil.toBean(goodsVO, GoodsPO.class));
        return R.ok();
    }

    @Operation(summary = "编辑商品")
    @PutMapping
    public R update(@RequestBody @Validated(value = {UpdateGroup.class}) GoodsVO goodsVO) {
        service.updateById(BeanUtil.toBean(goodsVO, GoodsPO.class));
        return R.ok();
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/{id}")
    public R delById(@PathVariable(value = "id", required = true) String id) {
        service.delById(id);
        return R.ok();
    }

}
